package com.wei.duowan.bbs.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.google.gson.Gson;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.NewsChannelBean;
import com.wei.duowan.bbs.model.TestBean;
import com.wei.duowan.bbs.presenter.BasePresenter;
import com.wei.duowan.bbs.ui.activity.BaseActivity;
import com.wei.duowan.bbs.ui.activity.NewListActivity;
import com.wei.duowan.bbs.ui.adapter.BaseDelegateAdapter;
import com.wei.duowan.bbs.ui.adapter.BaseRecyclerViewAdapter;
import com.wei.duowan.bbs.ui.adapter.BaseViewHolder;
import com.wei.duowan.bbs.ui.adapter.NewsAdapter;
import com.wei.duowan.bbs.utils.Configuration;
import com.wei.duowan.bbs.utils.StringUtils;

import java.util.List;

import butterknife.Bind;

/**
 * 作者：赵若位
 * 时间：2019/8/26 21:10
 * 联系方式：1070138445
 * 功能：
 */
public class NewsListFragment extends BaseLazyFragment
{
    private static final String TAG = NewsListFragment.class.getSimpleName();

    @Bind(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private NewsChannelBean mData;

    private DelegateAdapter mAdapter;
    private NewsAdapter mNewsAdapter;
    private BaseDelegateAdapter mMenuAdapter;

    /*传递参数*/
    public static NewsListFragment getFragment(@Nullable NewsChannelBean parameter)
    {
        NewsListFragment fragment = new NewsListFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(TAG, parameter);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected BasePresenter createPresenter()
    {
        return null;
    }

    @Override
    protected int getLayoutID()
    {
        return R.layout.fragment_news_list;
    }

    @Override
    protected void initView()
    {
        mData = (getArguments() != null && getArguments().getParcelable(TAG) != null) ? (NewsChannelBean) getArguments().getParcelable(TAG) : null;
        VirtualLayoutManager manager = new VirtualLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        ((SimpleItemAnimator) mRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        mAdapter = new DelegateAdapter(manager);
        mRecyclerView.setAdapter(mAdapter);


        if (mData != null && mData.getChannel() != null && mData.getChannel().size() != 0)
        {
            final List<NewsChannelBean.ChannelBean> list = mData.getChannel();
            mMenuAdapter = new BaseDelegateAdapter<Integer>(mActivity, R.layout.layout_recyclerview)
            {
                @Override
                protected void convert(@Nullable BaseViewHolder holder, int position, @Nullable Integer integer)
                {
                    RecyclerView recyclerView = holder.getView(R.id.recyclerView);
                    recyclerView.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.HORIZONTAL, false));
                    BaseRecyclerViewAdapter adapter = new BaseRecyclerViewAdapter<NewsChannelBean.ChannelBean>(mActivity, R.layout.layout_news_menu)
                    {
                        @Override
                        protected void convert(int position, @Nullable BaseViewHolder holder, @Nullable final NewsChannelBean.ChannelBean data)
                        {
                            ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
                            params.width = Configuration.getInstance().getScreenWidth() / (list.size() > 6 ? 6 : list.size());
                            holder.setImageResource(R.id.img_menu, data.getImg())
                                    .setText(R.id.tv_title, data.getTitle())
                                    .itemView.setLayoutParams(params);
                            holder.itemView.setOnClickListener(view ->
                            {
                                ((BaseActivity)mActivity).openActivity(NewListActivity.class,data);
                            });
                        }
                    };
                    adapter.bindRecyclerView(recyclerView);
                    adapter.setNewData(list);
                }
            };
            mAdapter.addAdapter(mMenuAdapter);
            mMenuAdapter.setNewData(1);
        }
        mNewsAdapter = new NewsAdapter(mActivity);
        mNewsAdapter.setIsEmpty(true);
        mAdapter.addAdapter(mNewsAdapter);
    }

    @Override
    public void initData()
    {
        Log.e(TAG, "initData: 开始加载数据");
        String json = StringUtils.getAssetsJson(mData.getName() + ".json");
        if (TextUtils.isEmpty(json))
        {
            return;
        }
        Gson gson = new Gson();
        TestBean test = gson.fromJson(json, TestBean.class);
        if (test != null && test.getNews() != null && test.getNews().size() != 0)
        {
            mNewsAdapter.setNewData(test.getNews());
        }
    }
}
